/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package gui.forums.addedit;

import domain.DomainController;
import domain.enums.Action;
import domain.forumdata.Forum;
import domain.forumdata.ForumCategory;
import domain.forumdata.SupportedForums;
import domain.forumdata.forums.Generic;
import domain.ui.AddEditForumUI;
import gui.forums.SupportedForumsPopupFrame;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.logging.Level;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import persistence.net.URLChecker;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 */
public class AddEditForumFrame extends javax.swing.JFrame implements AddEditForumUI {

    private DefaultListModel model;
    private DefaultComboBoxModel cmbModel;
    private boolean isEdit = false;
    private Forum editForum = null;

    private SupportedForumsPopupFrame popup;

    /** Creates new form AddEditForumFrame */
    public AddEditForumFrame()
    {
        this(Action.ADD);
    }

    public AddEditForumFrame(Action action)
    {
        DomainController.getInstance().initTempForumCategories();
        initComponents();
        String actionString = "";

        this.cmbModel = new DefaultComboBoxModel(SupportedForums.SUPPORTED_FORUMS);
        this.cmbForumType.setModel(this.cmbModel);
        this.cmbForumType.setSelectedIndex(0);

        switch(action)
        {
            case ADD:
                isEdit = false;
                actionString = "Add";
                break;
            case EDIT:
                isEdit = true;
                actionString = "Edit";
                this.loadEdit();
                break;
        }
        DomainController.getInstance().setAddEditForumUI(this);
        this.setTitle(actionString + " forum");
        this.setIconImage(Toolkit.getDefaultToolkit().getImage("/data/icon.png"));
        this.checkCats();
        this.popup = new SupportedForumsPopupFrame();
    }

    private void loadEdit()
    {
        //We are editing, so only one forum is selected, thus index [0].
        String selectedForum = DomainController.getInstance().getSelectedForums()[0];

        this.editForum = DomainController.getInstance().getForums().getForumByName(selectedForum);

        this.cmbForumType.setSelectedItem(this.editForum.getForumType());
        this.txtForumName.setText(this.editForum.getForumName());
        this.txtLoginUrl.setText(this.editForum.getLoginURL());
        this.txtUsername.setText(this.editForum.getUsername());
        this.txtPassword.setText(new String(this.editForum.getPassword()));
        DomainController.getInstance().setTempForumCategories(this.editForum.getCategories());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblForumType = new javax.swing.JLabel();
        lblInfo = new javax.swing.JLabel();
        lblUsername = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        lblPassword = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstAddedCategories = new javax.swing.JList();
        cmdAdd = new javax.swing.JButton();
        cmdRemove = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        cmdSave = new javax.swing.JButton();
        cmbForumType = new javax.swing.JComboBox();
        lblLoginURL = new javax.swing.JLabel();
        txtLoginUrl = new javax.swing.JTextField();
        lblForumName = new javax.swing.JLabel();
        txtForumName = new javax.swing.JTextField();
        cmdTest = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        lblForumType.setText("Forum type:");
        lblForumType.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblForumTypeMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblForumTypeMouseEntered(evt);
            }
        });
        lblForumType.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                lblForumTypeMouseMoved(evt);
            }
        });

        lblInfo.setText("Add categories along with their \"new topic\" URL below:");

        lblUsername.setText("Username:");

        lblPassword.setText("Password:");

        lstAddedCategories.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(lstAddedCategories);

        cmdAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/data/add_32.png"))); // NOI18N
        cmdAdd.setToolTipText("Add");
        cmdAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdAddActionPerformed(evt);
            }
        });

        cmdRemove.setIcon(new javax.swing.ImageIcon(getClass().getResource("/data/remove_32.png"))); // NOI18N
        cmdRemove.setToolTipText("Remove");
        cmdRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdRemoveActionPerformed(evt);
            }
        });

        cmdSave.setText("Save");
        cmdSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdSaveActionPerformed(evt);
            }
        });

        cmbForumType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbForumType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbForumTypeActionPerformed(evt);
            }
        });

        lblLoginURL.setText("Login URL:");

        txtLoginUrl.setEnabled(false);

        lblForumName.setText("Forum name:");

        cmdTest.setText("Test");
        cmdTest.setToolTipText("Tries to login at the forum. If the test is positive, the forum will most likely work.");
        cmdTest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdTestActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(257, 257, 257)
                                .add(cmdAdd, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 63, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(cmdRemove, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 63, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jSeparator2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
                            .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, lblInfo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(cmdTest, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 94, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 197, Short.MAX_VALUE)
                                .add(cmdSave, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 96, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(lblLoginURL, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                                    .add(lblUsername, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                                    .add(lblPassword))
                                .add(219, 219, 219)))
                        .add(6, 6, 6))
                    .add(lblForumName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 173, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(layout.createSequentialGroup()
                        .add(lblForumType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 164, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cmbForumType, 0, 224, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(172, 172, 172)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(txtLoginUrl, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                            .add(txtForumName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                            .add(txtUsername, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                            .add(txtPassword, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblForumType)
                    .add(cmbForumType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblForumName)
                    .add(txtForumName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblLoginURL)
                    .add(txtLoginUrl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblUsername)
                    .add(txtUsername, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblPassword)
                    .add(txtPassword, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(lblInfo)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 118, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(cmdRemove)
                    .add(cmdAdd))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cmdSave)
                    .add(cmdTest))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmdAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdAddActionPerformed

        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                CategoryFrame cf = new CategoryFrame();
                cf.setLocationRelativeTo(null);
                cf.setVisible(true);
            }
        });
        
    }//GEN-LAST:event_cmdAddActionPerformed

    private void cmdRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdRemoveActionPerformed

        if(!this.lstAddedCategories.isSelectionEmpty())
            DomainController.getInstance().removeTempForumCategory(this.lstAddedCategories.getSelectedValue().toString());

}//GEN-LAST:event_cmdRemoveActionPerformed

    private void cmdSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSaveActionPerformed

        String forumType = (String)this.cmbForumType.getSelectedItem();
        String forumName = this.txtForumName.getText();
        String loginURL = this.txtLoginUrl.getText();
        String username = this.txtUsername.getText();
        char[] password = this.txtPassword.getPassword();
        String err = "";

        if(forumName.length() < 1)
        {
            err = "Forum name is invalid.";
            DomainController.getInstance().getLogger().log(Level.WARNING, err);
            DomainController.getInstance().fireMessage(err, "Error");
            this.txtForumName.requestFocus();
        }
        //Name must be unique!
        else if(!isEdit && DomainController.getInstance().getForums().isInList(forumName))
        {
            err = "The forum name already exists.";
            DomainController.getInstance().getLogger().log(Level.WARNING, err);
            DomainController.getInstance().fireMessage(err, "Error");
            this.txtForumName.requestFocus();
        }
        else if(!URLChecker.check(loginURL, true))
        {
            //Errors shown by the url checker.
            this.txtLoginUrl.requestFocus();
        }
        else if(username.length() < 1 || password.length < 1)
        {
            err = "Username or password is invalid.";
            DomainController.getInstance().getLogger().log(Level.WARNING, err);
            DomainController.getInstance().fireMessage(err, "Error");
            this.txtUsername.requestFocus();
        }
        else
        {
            if(this.model.getSize() < DomainController.getInstance().getCategories().getCategories().size())
            {
                err = "You haven't added all categories. Do you want to continue?";
                if(JOptionPane.showConfirmDialog(this, err, "Attention", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
                {
                    //All ok.
                    doSave(forumType, forumName, username, password);
                } //else exit this method.
            }
            else
            {
                //All ok.
                doSave(forumType, forumName, username, password);
            }
        }
    }//GEN-LAST:event_cmdSaveActionPerformed

    private void cmbForumTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbForumTypeActionPerformed

        String forumType = (String)this.cmbForumType.getSelectedItem();

        int numberOfTypes = DomainController.getInstance().getNumberOfForumsOfType(forumType);
        String forumName = forumType + (numberOfTypes > 0 ? " (" + numberOfTypes + ")" : "");
        this.txtForumName.setText(forumName);

        if(forumType.equals(SupportedForums.SUPPORTED_FORUMS[0]))
        {
            //Generic!!!
            this.txtLoginUrl.setEnabled(true);
            this.txtLoginUrl.setText("");
            this.txtLoginUrl.setToolTipText(null);
        }
        else
        {
            //Not supported forum, empty and enable.
            this.txtLoginUrl.setEnabled(true);
            this.txtLoginUrl.setText("");
            this.txtLoginUrl.setToolTipText(null);
        }

    }//GEN-LAST:event_cmbForumTypeActionPerformed

    private void cmdTestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdTestActionPerformed

        String forumType = (String)this.cmbForumType.getSelectedItem();
        String forumName = this.txtForumName.getText();
        String loginURL = this.txtLoginUrl.getText();
        String username = this.txtUsername.getText();
        char[] password = this.txtPassword.getPassword();
        String err = "";

        if(!URLChecker.check(loginURL, true))
        {
            //Errors shown by the url checker.
            this.txtLoginUrl.requestFocus();
        }
        else if(username.length() < 1 || password.length < 1)
        {
            err = "Username or password is invalid.";
            DomainController.getInstance().getLogger().log(Level.WARNING, err);
            DomainController.getInstance().fireMessage(err, "Error");
            this.txtUsername.requestFocus();
        }
        else
        {
            Forum forum = null;

            if(forumType.equals(SupportedForums.SUPPORTED_FORUMS[0]))
            {
                //Generic!
                forum = new Generic(forumName, this.txtLoginUrl.getText(), username, password, DomainController.getInstance().getTempForumCategories());
            }

            if(forum == null)
            {
                JOptionPane.showMessageDialog(null, "Test failed!", "Test", JOptionPane.ERROR_MESSAGE);
            }
            else
            {
                this.setButtonsEnabled(false);
                DomainController.getInstance().testForum(forum);
            }
        }      
    }//GEN-LAST:event_cmdTestActionPerformed

    private void lblForumTypeMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblForumTypeMouseMoved

        this.updatePopupLocation(evt);
        this.popup.setVisible(true);

    }//GEN-LAST:event_lblForumTypeMouseMoved

    private void lblForumTypeMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblForumTypeMouseEntered

        this.updatePopupLocation(evt);

    }//GEN-LAST:event_lblForumTypeMouseEntered

    private void lblForumTypeMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblForumTypeMouseExited

        this.popup.setVisible(false);

    }//GEN-LAST:event_lblForumTypeMouseExited

    private void updatePopupLocation(java.awt.event.MouseEvent evt)
    {
        int space = 5;
        int frame_width = this.popup.getWidth();
        int frame_height = this.popup.getHeight();
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = evt.getXOnScreen() + space;
        int y = evt.getYOnScreen() + space;
        
        if(x + frame_width > screen.getWidth() && y + frame_height > screen.getHeight())
        {
            //Left above
            x = evt.getXOnScreen() - space - frame_width;
            y = evt.getYOnScreen() - space - frame_height;
        }
        else if(x + frame_width > screen.getWidth())
        {
            //Left below
            x = evt.getXOnScreen() - space - frame_width;
        }
        else if(y + frame_height > screen.getHeight())
        {
            //Right above
            y = evt.getYOnScreen() - space - frame_height;
        }
        //else ok -> right below
        
        this.popup.setLocation(x, y);
    }

    public void testCompleted(boolean success)
    {
        this.setButtonsEnabled(true);
        String err = "";
        if(success)
        {
            err = "Test passed!";
            DomainController.getInstance().getLogger().log(Level.INFO, err);
            JOptionPane.showMessageDialog(null, err, "Test", JOptionPane.INFORMATION_MESSAGE);
        }
        else
        {
            err = "Test failed!";
            DomainController.getInstance().getLogger().log(Level.SEVERE, err);
            JOptionPane.showMessageDialog(null, err, "Test", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void setButtonsEnabled(boolean b)
    {
        this.cmbForumType.setEnabled(b);
        this.txtForumName.setEnabled(b);
        this.txtLoginUrl.setEnabled(b);
        this.txtPassword.setEnabled(b);
        this.txtUsername.setEnabled(b);
        this.lstAddedCategories.setEnabled(b);

        this.cmdAdd.setEnabled(b);
        this.cmdRemove.setEnabled(b);
        this.cmdSave.setEnabled(b);
        this.cmdTest.setEnabled(b);

        if(b)
            this.checkCats();
    }
    
    private void doSave(String forumType, String forumName, String username, char[] password)
    {
        if(isEdit)
            DomainController.getInstance().getForums().removeForum(this.editForum);

        //Get the right forum...
        Forum forum = null;

        if(forumType.equals(SupportedForums.SUPPORTED_FORUMS[0]))
        {
            //Generic!
            forum = new Generic(forumName, this.txtLoginUrl.getText(), username, password, DomainController.getInstance().getTempForumCategories());
        }
        else
        {
            //Not supported, TODO?
        }

        DomainController.getInstance().getForums().addForum(forum);
        this.dispose();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cmbForumType;
    private javax.swing.JButton cmdAdd;
    private javax.swing.JButton cmdRemove;
    private javax.swing.JButton cmdSave;
    private javax.swing.JButton cmdTest;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel lblForumName;
    private javax.swing.JLabel lblForumType;
    private javax.swing.JLabel lblInfo;
    private javax.swing.JLabel lblLoginURL;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblUsername;
    private javax.swing.JList lstAddedCategories;
    private javax.swing.JTextField txtForumName;
    private javax.swing.JTextField txtLoginUrl;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables

    private void checkCats()
    {
        this.categoryAddedOrRemoved();
    }
    
    public void categoryAddedOrRemoved()
    {
        this.model = new DefaultListModel();

        for(ForumCategory fc : DomainController.getInstance().getTempForumCategories())
            this.model.addElement(fc.getCategory().getName());

        this.lstAddedCategories.setModel(this.model);

        //Check the buttons...
        this.cmdAdd.setEnabled(DomainController.getInstance().getTempForumCategories().size() < DomainController.getInstance().getCategories().getCategories().size());
        this.cmdRemove.setEnabled(this.model.size() > 0);
    }
}